import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
plt.subplots_adjust(hspace=0.5, wspace=1)
plt.subplot(2, 3, 4)
plt.plot(0, 0, 'g*', ms=50)
plt.title("(2, 3, 3)")
plt.subplot(2, 3, 1)
plt.plot(0, 0, 'ro', ms=50)
plt.title("(2, 3, 1)")
plt.subplot(2, 3, 2)
plt.plot(0, 0, 'b+', ms=50)
plt.title("(2, 3, 3)")
plt.subplot(2, 3, 3)
plt.title("(2, 3, 3)");
plt.show()
from ipywidgets import interact, fixed
def multiply(a, b):
return a*b
interact(multiply, a=(1, 20), b=(1,10));
interact(multiply, a=(1, 20), b=fixed(10));
import numpy as np
def f(n, color, sin, cos):
x = np.linspace(0, 7, 100)
if sin:
plt.plot(x, np.sin(n*x), '--', color=color)
if cos:
plt.plot(x, np.cos(n*x), '-', color=color)
plt.show()
f(n=1, color='r', sin=True, cos=True)
interact(f, n=(1, 10), color=['r', 'b', 'g'], sin=True, cos=True);
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=200,
n_features=2,
centers=4,
cluster_std=1.5,
random_state=10
)
plt.scatter(X[:, 0], X[:, 1]);
from sklearn.datasets import make_moons
X, y = make_moons(n_samples=200, noise=0.07)
plt.scatter(X[:, 0], X[:, 1]);
Note: With this algorithm we need to specify how many clusters we want to obtain.
n data points as initial cluster centroids. Repeat steps 1 and 2 until cluster centroids are not changing anymore.
%config InlineBackend.figure_format = 'retina'
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def get_distances(X, Y):
"""
Given 2-dimensional numpy arrays X and Y
returns a 2-dimensional array whose (i, j)-entry
is the square of the Euclidean distance between
the i-th row of X and the j-th row of Y
"""
return np.sum((X[:, np.newaxis, :] - Y[np.newaxis, :, :])**2, axis=-1)
def kmeans(X, n):
"""
Implements the k-mneans algorithm.
X:
2-dimensional numpy array whose rows are coordinates of data points.
n:
Integer, the number of clusters.
Returns:
A list of tuples (labels, centers), one tuple for each iteration step.
labels is a 1-dimensional array with labels od points in X, centers is
a 2-dimensional array with coordinates of centroids of clusters.
The last tuple on the list is the clustering final result.
"""
X = X.copy()
h = X.shape[0]
centers = X[np.random.choice(h, size=n, replace=False)]
new_centers = np.zeros_like(centers)
steps = []
while True:
d = get_distances(X, centers)
labels = np.argmin(d, axis=1)
steps.append((labels, centers))
for j in range(n):
new_centers[j] = np.mean(X[labels == j], axis=0)
if np.array_equal(centers, new_centers):
break
else:
centers = new_centers.copy()
return steps
def plot_clusters(X, labels=None, centers=None):
"""
Plots clusters and their centoids.
X:
2-dimensional numpy array whose rows are coordinates of data points.
labels:
1-dimensional numpy array with labels of points in X.
centers:
2-dimensional numpy array whose rows are coordinates of cluster
centroids.
"""
if labels is not None:
n = len(set(labels))
else:
n = 1
colors = ListedColormap(
['tab:blue', 'tab:red', 'tab:green', 'tab:orange', 'tab:purple'][:n])
plt.figure(figsize=(6, 6))
plt.scatter(X[:, 0], X[:, 1], c=labels, marker="+", cmap=colors)
if centers is not None:
plt.scatter(centers[:, 0],
centers[:, 1],
c=range(centers.shape[0]),
s=250,
linewidths=3,
edgecolors="k",
cmap=colors)
plt.show()
def plot_iteration(X, steps, k=0):
"""
Plots clusters computed by a given iteration of k-means.
X:
2-dimensional numpy array whose rows are coordinates of data points.
steps:
A list with cluster data produced by the kmeans function.
k:
An index of the steps list
"""
plot_clusters(X, steps[k][0], steps[k][1])
X, y = make_blobs(n_samples=600,
n_features=2,
centers=3,
cluster_std=2,
random_state=100
)
plt.figure(figsize=(6,6))
plt.plot(X[:, 0], X[:, 1], "+");
n=3
steps = kmeans(X, 3)
steps[1]
(array([2, 0, 2, 2, 0, 2, 1, 1, 1, 0, 1, 2, 2, 0, 0, 2, 2, 1, 0, 1, 1, 1,
0, 2, 0, 2, 0, 0, 2, 2, 2, 2, 1, 0, 1, 1, 2, 2, 1, 0, 0, 1, 1, 1,
1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 2, 0, 2, 2, 0, 1, 0, 1, 2, 0, 2, 2,
2,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2, 2, 1, 1, 2, 2, 1, 2, 2, 0,
2, 0, 1, 0, 0, 2, 0, 2, 0, 1, 1, 1, 2, 2, 0, 2, 1, 2, 1, 0, 1, 0,
0, 1, 2, 0, 0, 0, 2, 1, 1, 2, 0, 2, 0, 2, 1, 1, 1, 2, 2, 2, 1, 2,
0, 2, 1, 0, 2, 2, 2, 2, 0, 0, 1, 0, 1, 1, 2, 2, 2, 1, 2, 0, 2, 1,
1, 1, 1, 1, 1, 2, 0, 0, 2, 1, 0, 2, 0, 0, 2, 0, 1, 2, 1, 0, 1, 1,
1, 0, 0, 1, 2, 0, 0, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 0, 0, 0, 0, 1,
2, 0, 0, 1, 2, 2, 2, 0, 2, 1, 0, 2, 0, 1, 2, 1, 1, 1, 1, 1, 1, 2,
1, 1, 0, 1, 0, 2, 2, 1, 2, 0, 2, 0, 0, 0, 0, 1, 2, 0, 0, 2, 0, 2,
1, 1, 2, 2, 0, 0, 2, 2, 1, 0, 2, 1, 2, 1, 2, 2, 2, 1, 0, 1, 2, 1,
1, 1, 0, 0, 2, 0]),
array([[-9.81224097, -7.55995225],
[ 1.90912978, -5.75234818],
[-1.10438125, 3.62000033]]))
len(steps)
5
plot_iteration(X, steps, k=2)
X, y = make_blobs(n_samples=600,
n_features=2,
centers=3,
cluster_std=1.5,
#random_state=100
)
steps = kmeans(X, 3)
interact(plot_iteration, X=fixed(X), steps=fixed(steps), k=(0, len(steps)-1));
X, y = make_moons(n_samples=200, noise=0.07)
steps = kmeans(X, 2)
interact(plot_iteration, X=fixed(X), steps=fixed(steps), k=(0, len(steps)-1));
from sklearn.cluster import KMeans
km = KMeans(n_clusters=3, init="k-means++")
km
KMeans(n_clusters=3)
X, y = make_blobs(n_samples=600,
n_features=2,
centers=3,
cluster_std=1.5,
random_state=100
)
X, y
(array([[ -2.90852337, 5.88517581],
[ -8.14220866, -6.84907327],
[ 0.04981016, 5.49906817],
...,
[ -3.59187438, -4.38263638],
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, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 0, 0, 2, 0, 0, 1,
0, 1, 2, 1, 1, 0, 1, 0, 1, 2, 2, 2, 0, 0, 1, 0, 2, 0, 2, 1, 2, 1,
1, 2, 0, 1, 1, 1, 0, 2, 2, 0, 1, 0, 1, 0, 2, 2, 2, 0, 2, 0, 2, 0,
1, 0, 2, 1, 0, 0, 0, 0, 1, 1, 2, 1, 2, 2, 0, 0, 0, 2, 0, 1, 0, 2,
2, 2, 2, 2, 2, 0, 1, 1, 0, 2, 1, 0, 1, 1, 0, 1, 2, 0, 2, 1, 2, 2,
2, 1, 1, 2, 0, 1, 1, 1, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 1, 1, 1, 2,
0, 1, 1, 2, 0, 0, 0, 1, 0, 2, 1, 0, 1, 2, 0, 2, 2, 2, 2, 2, 2, 0,
2, 2, 1, 2, 1, 2, 2, 2, 0, 1, 0, 1, 1, 1, 1, 2, 0, 1, 1, 0, 1, 0,
2, 2, 0, 0, 1, 1, 0, 0, 2, 1, 0, 2, 0, 2, 0, 0, 0, 2, 1, 2, 0, 2,
2, 2, 1, 2, 0, 1], dtype=int32)
km.cluster_centers_
array([[-1.52178358, 6.76079726],
[-9.88845179, -7.54260416],
[ 0.91160521, -4.46687065]])
plot_clusters(X, km.labels_, km.cluster_centers_)
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
knn
KNeighborsClassifier()
X, y = make_blobs(n_samples=600,
n_features=2,
centers=3,
cluster_std=3,
random_state=100
)
knn.fit(X, y)
knn
KNeighborsClassifier()
p = [[1, 0]]
arr = np.array(p).reshape(1, 2)
arr
array([[1, 0]])
label = knn.predict(arr)
label
array([0])
%config InlineBackend.figure_format = 'retina'
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, Normalize
def plot_clusters(X, y, p=None, p_label=None, neighbors=None):
"""
Plots nearest neighbors of a given point.
X:
A 2-dimensional numpy array with coordinates
of points in clusters
y:
A 1-dimensional numpy array with labels of points
p:
An array with coordinates of the point whose
neighbors will be plotted.
p_label:
The predicted label of the point p.
neighbors:
A list of row numbers of X which are the nearest
neighbors of the point p.
"""
# build a custom colormap
col_list = ['dodgerblue', 'limegreen', 'red', 'orange', 'fuchsia']
colors = ListedColormap(col_list)
norm = Normalize(0, len(col_list))
plt.figure(figsize=(8, 8))
with plt.style.context('seaborn'):
scatter = plt.scatter(X[:, 0],
X[:, 1],
c=y,
s=90,
cmap=colors,
norm=norm,
label=y)
if p is not None:
p_col = "k" if p_label is None else colors(norm(p_label))
plt.plot(p[0], p[1], marker="*", mfc=p_col, mec="k", ms=30, mew=2)
if neighbors is not None:
plt.scatter(X[neighbors, 0],
X[neighbors, 1],
edgecolors='black',
linewidth=3,
facecolors="None",
s=300)
plt.legend(*scatter.legend_elements(),
markerscale=1.5,
prop={
"size": 12,
"weight": "normal"
})
plt.show()
plot_clusters(X, y, p=[1, 0], p_label=0)
X, y = make_blobs(n_samples=600,
n_features=2,
centers=4,
cluster_std=3,
random_state=100
)
N = 500
train_X = X[:N]
train_y = y[:N]
test_X = X[N:]
test_y = y[N:]
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(train_X, train_y )
KNeighborsClassifier()
predictions = knn.predict(test_X)
predictions
array([2, 3, 0, 2, 3, 3, 3, 1, 0, 1, 2, 0, 1, 3, 1, 2, 1, 1, 0, 1, 0, 2,
1, 3, 0, 2, 3, 2, 0, 0, 1, 3, 2, 1, 1, 1, 1, 1, 3, 0, 1, 2, 3, 0,
2, 0, 0, 0, 1, 0, 3, 1, 0, 1, 2, 0, 3, 0, 0, 0, 1, 3, 2, 3, 1, 1,
3, 0, 1, 0, 2, 1, 0, 1, 0, 0, 2, 1, 1, 3, 2, 1, 0, 2, 2, 1, 1, 0,
1, 1, 1, 1, 3, 0, 2, 0, 3, 0, 1, 0])
test_y
array([2, 3, 0, 2, 1, 3, 1, 1, 0, 3, 2, 0, 1, 3, 3, 2, 1, 1, 0, 1, 0, 2,
1, 1, 0, 2, 3, 2, 3, 0, 1, 3, 2, 1, 1, 1, 1, 3, 1, 0, 3, 2, 3, 0,
2, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 0, 3, 0, 0, 0, 1, 3, 2, 3, 3, 3,
3, 0, 1, 3, 2, 1, 3, 1, 0, 0, 2, 1, 3, 3, 2, 1, 0, 2, 2, 1, 1, 0,
1, 1, 1, 1, 3, 0, 2, 0, 1, 0, 3, 0])
predictions == test_y
array([ True, True, True, True, False, True, False, True, True,
False, True, True, True, True, False, True, True, True,
True, True, True, True, True, False, True, True, True,
True, False, True, True, True, True, True, True, True,
True, False, False, True, False, True, True, True, True,
True, True, True, False, True, False, True, True, True,
True, True, True, True, True, True, True, True, True,
True, False, False, True, True, True, False, True, True,
False, True, True, True, True, True, False, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, False, True, False,
True])
(predictions == test_y).sum()
82
(predictions == test_y).sum()/len(predictions)
0.82
confusion = np.zeros((4, 4), dtype=int)
confusion
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
for lab, pred in zip(test_y, predictions):
confusion[lab, pred] += 1
confusion
array([[27, 1, 0, 0],
[ 0, 25, 0, 6],
[ 0, 0, 18, 0],
[ 3, 8, 0, 12]])
import seaborn as sns
sns.heatmap(confusion,
cmap = "Blues_r",
annot=True,
annot_kws={"fontsize":16},
linewidth=2,
);
pwd
'/Users/bb/Documents/GitHub/MTH548/preps/week_3'
ls
butterfly.jpg tiger.jpg week_3.ipynb peppers.jpg town.jpg week_3_prep.ipynb
a = np.random.randint(0, 100, (4, 4))
a
array([[76, 95, 83, 58],
[90, 48, 41, 38],
[ 8, 81, 30, 56],
[19, 86, 73, 40]])
plt.imshow(a, cmap="Reds")
<matplotlib.image.AxesImage at 0x7fa7fdafc550>
b = np.random.randint(0, 255, (4, 4, 3))
b
array([[[ 31, 7, 163],
[171, 7, 224],
[106, 87, 91],
[199, 186, 209]],
[[ 99, 201, 206],
[228, 212, 21],
[ 36, 254, 5],
[219, 85, 224]],
[[236, 243, 56],
[171, 187, 126],
[107, 191, 111],
[191, 160, 109]],
[[126, 123, 34],
[135, 203, 199],
[244, 250, 45],
[225, 180, 29]]])
plt.imshow(b)
<matplotlib.image.AxesImage at 0x7fa7fdc2a7c0>
tiger = plt.imread("tiger.jpg")
tiger
array([[[243, 224, 194],
[242, 223, 193],
[236, 217, 187],
...,
[241, 218, 184],
[245, 222, 188],
[250, 227, 193]],
[[246, 229, 199],
[246, 229, 199],
[241, 224, 194],
...,
[247, 224, 190],
[252, 229, 195],
[252, 229, 195]],
[[249, 232, 204],
[251, 234, 206],
[249, 232, 204],
...,
[253, 230, 198],
[255, 233, 201],
[248, 225, 193]],
...,
[[254, 251, 244],
[254, 251, 244],
[254, 251, 244],
...,
[254, 247, 237],
[254, 247, 237],
[254, 247, 237]],
[[254, 251, 244],
[254, 251, 244],
[254, 251, 244],
...,
[254, 247, 237],
[254, 247, 237],
[254, 247, 237]],
[[254, 251, 244],
[254, 251, 244],
[254, 251, 244],
...,
[254, 247, 237],
[254, 247, 237],
[254, 247, 237]]], dtype=uint8)
tiger.shape
(644, 1000, 3)
plt.figure(figsize=(16,16))
plt.imshow(tiger)
<matplotlib.image.AxesImage at 0x7f9016e26ac0>
tiger2d = tiger.reshape( -1, 3)
tiger2d
array([[243, 224, 194],
[242, 223, 193],
[236, 217, 187],
...,
[254, 247, 237],
[254, 247, 237],
[254, 247, 237]], dtype=uint8)
tiger2d.shape
(644000, 3)
np.unique(tiger2d, axis=0).shape
(85796, 3)